package cn.edu.pku.ss.music.activities;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.pku.ss.music.R;
import cn.edu.pku.ss.music.net.RegisterTask;
import cn.edu.pku.ss.music.util.ConnectionURLs;
import cn.edu.pku.ss.music.util.ErrorCode;
import cn.edu.pku.ss.music.util.Util;

public class RegisterActivity extends Activity {
	private EditText register_input_name;
	private EditText register_input_password;
	private EditText register_input_confirm_password;

	private ImageView register_profile_image;
	private ProgressBar loading;

	private Button register_button;
	private Button confirm_button;

	private TextView header_textview;

	private Dialog dialog;
	private View dialog_view;
	private String url = ConnectionURLs.REGISTER;

	private int register_result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);

		register_input_name = (EditText) findViewById(R.id.registration_input_name);
		register_input_password = (EditText) findViewById(R.id.registration_input_password);
		register_input_confirm_password = (EditText) findViewById(R.id.registration_input_password_confirm);
		register_profile_image = (ImageView) findViewById(R.id.registration_profile_image);
		register_button = (Button) findViewById(R.id.register_btn);
		
		dialog_view = getLayoutInflater().inflate(R.layout.register_dialog_layout, null);
		confirm_button = (Button) dialog_view.findViewById(R.id.registration_dialog_ok);

		View header = findViewById(R.id.register_header);
		header_textview = (TextView) header.findViewById(R.id.header_title);
		loading = (ProgressBar) header.findViewById(R.id.loading);

		header_textview.setText(R.string.register);

		confirm_button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				loading.setVisibility(View.GONE);
				RegisterActivity.this.finish();
				startActivity(new Intent(RegisterActivity.this, PortalActivity.class));
			}
		});

		register_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String username = register_input_name.getText().toString();
				String password = register_input_password.getText().toString();
				String password_confirm = register_input_confirm_password
						.getText().toString();

				if (Util.isEmpty(username)) {
					Toast.makeText(RegisterActivity.this,
							R.string.empty_username, Toast.LENGTH_SHORT).show();
					return;
				}
				if (Util.isEmpty(password) || Util.isEmpty(password_confirm)) {
					Toast.makeText(RegisterActivity.this,
							R.string.empty_password, Toast.LENGTH_SHORT).show();
					return;
				}
				if (!(password.equals(password_confirm))) {
					Toast.makeText(RegisterActivity.this,
							R.string.password_dismatch, Toast.LENGTH_SHORT)
							.show();
					return;
				}

				loading.setVisibility(View.VISIBLE);
				RegisterTask register_task = new RegisterTask();
				register_task.execute(url, username, password);
				try {
					register_result = register_task.get();
					if (register_result == ErrorCode.NO_ERROR) {
//						RegisterActivity.this.finish();

						dialog = new Dialog(RegisterActivity.this,
								R.style.dialog);
						dialog.setContentView(dialog_view);
						dialog.setCanceledOnTouchOutside(true);
						dialog.show();
					} else {
						// error

					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}
		});

		register_profile_image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

			}
		});
	}

}
